<script>
	import Vue from 'vue'
	import {
		mapActions,
		mapMutations
	} from 'vuex'
	var _self
	export default {
		created() {

		},
		methods: {
			getFutureInstrumentInfo(cleanCache) {
				if (_self.getDefaultContract() != null && _self.getContractMap() != null && _self.getContractMap().size > 0 &&
					_self.getMarkets() != null && _self.getMarkets() != "" && !cleanCache) {
					console.log("default is exists")

					console.log(_self.getDefaultContract())
					console.log(_self.getContractMap())
					console.log(_self.getMarkets())
				} else {
					uni.request({
						url: _self.api + '/apis/okex/unauth?simpleUrl=/api/futures/v3/instruments',
						method: 'GET',
						success: res => {
							console.log('getFutureInstrumentInfo:')
							console.log(res.data.data)
							var marketList = [
								[],
								[],
								[],
								[]
							]
							var defaulContract = {}
							var contractMap = []
							for (let item of res.data.data) {
								if (item.quote_currency === "USD") {
									if (item.alias === "quarter") {
										if (item.underlying == "BTC-USD") {
											defaulContract = item
										}
										marketList[2].push(item)
										contractMap.push(item)
									} else if (item.alias === "next_week") {
										marketList[1].push(item)
										contractMap.push(item)
									} else if (item.alias === "this_week") {
										marketList[0].push(item)
										contractMap.push(item)
									} else if (item.alias === "bi_quarter") {
										marketList[3].push(item)
										contractMap.push(item)
									}
								}
							}
							_self.cacheMarkets(marketList)
							if (_self.getDefaultContract() == '' || _self.getDefaultContract() == null) {
								_self.cacheDefaultContract(defaulContract)
							}
							_self.cacheContractMap(contractMap)
						}
					});
				}
			},
			checkApkVersion() {
				console.log("check apk version " + plus.runtime.version)
				// 锁定屏幕方向
				plus.screen.lockOrientation('portrait-primary'); //锁定
				// 检测升级
				console.log(_self.api + '/update/check');
				uni.request({
					url: _self.api + '/update/check', //检查更新的服务器地址
					method: 'POST',
					data: {
						appid: plus.runtime.appid,
						version: plus.runtime.version,
						imei: plus.device.imei
					},
					success: (res) => {
						if (res.statusCode == 200) {
							if (res.data.data.isUpdate) {
								console.log("check apk version reps ")
								console.log(res.data.data)
								let openUrl = plus.os.name === 'iOS' ? res.data.data.iosUrl : res.data.data.androidUrl;
								_self.setChannel(res.data.data.channelName)
								// 提醒用户更新L
								uni.showModal({
									title: '发现新版本',
									content: res.data.data.desc,
									success: (showResult) => {
										if (showResult.confirm) {
											console.log('plus open url is'+openUrl)
											plus.runtime.openURL(openUrl);
										}
									}
								})
							}else{
							  console.log('已是最新版本')
							}
						}
					}
				})
			},
			getSpotInstrumentInfo(cleanCache) {
				if (_self.getDefaultSpot() != null && _self.getSpotMarket() != null && _self.getSpotMarket().size > 0 && !
					cleanCache) {
					console.log("default spot is exists")
					console.log(_self.getDefaultSpot())
					console.log(_self.getSpotMarket())
				} else {
					uni.request({
						url: _self.api + '/instruments/spot',
						method: 'GET',
						success: res => {
							console.log('getSpotInstrumentInfo:')
							console.log(res.data.data)
							var marketList = [
								[]
								// [],
								// []
							]
							var defaulSpotInstument = {}
							for (let item of res.data.data) {
								if (item.quoteCurrency === "USDT") {
									if (item.baseCurrency == "BTC") {
										defaulSpotInstument = item
									}
									marketList[0].push(item)
								}
							}

							_self.cacheSpotMarket(marketList)
							_self.cacheDefaultSpot(defaulSpotInstument)
						}
					});
				}
			},
			initSysInfo() {
				uni.getSystemInfo({
					success: function(e) {
						Vue.prototype.StatusBar = e.statusBarHeight;
						if (e.platform == 'android') {
							Vue.prototype.CustomBar = e.statusBarHeight + 50;
						} else {
							Vue.prototype.CustomBar = e.statusBarHeight + 45;
						};
					}
				})
			}
		},
		onLaunch: function() {
			_self = this
			console.log("on launch")
			// uni.setStorageSync("DEFAULT_SPOT_MARKET", null)
			//#ifdef APP-PLUS
			this.checkApkVersion()
			//#endif
			this.initSysInfo()
			this.getFutureInstrumentInfo(true)
			this.getSpotInstrumentInfo(true)
		}

	}
</script>

<style>
	/* uni.css - 通用组件、模板样式库，可以当作一套ui库应用 */
	@import './common/uni.css';
	@import "colorui/main.css";
	@import "colorui/icon.css";


	/* 	html,
	body {
		overflow: hidden;
	} */

	.container {
		font-size: $font-base;
		/* overflow: hidden; */
	}

	.wrapper {
		font-size: $font-base;
		text-align: left;
	}

	* {
		transition: width 2s;
		-moz-transition: width 2s;
		-webkit-transition: width 2s;
		-o-transition: width 2s;
	}

	/* font color */
	* {
		color: #353535;
	}

	.gray {
		color: #808080;
	}

	.wxParse {
		text-indent: 2em;
	}

	.wxParse image {
		text-indent: 0;
	}

	.wxParse ._view {
		padding: 0 10rpx;
		text-indent: 0;
	}

	.wxParse .p {
		text-indent: 1em;
	}
</style>
